﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SocialLearning
{
    public class UAC_UserManagement
    {
        #region static Fields

        private static int Num = 0;

        #endregion

        #region  Private Fields

        public int uid { get; set; }
        //private UAC_UserManagement_Control umControl = new UAC_UserManagement_Control();
        private UAC_BasicDetails basic = null;
        private UAC_AdvancedDetails advanced = null;
        private UAC_EducationalBackground edu = null;
        private UAC_MarketingDetails marketingDetails = null;
        private UAC_Account SingleUser;

        #endregion

        #region Public Fields

        public static List<UAC_Account> userlist = new List<UAC_Account>();

        #endregion

        #region Registration Methods

        /// <summary>
        /// Basic Details.
        /// </summary>
        /// <param name="firstname">Firstname.</param>
        /// <param name="lastname">Lastname.</param>
        /// <param name="gender">Gender.</param>
        /// <param name="id">ID.</param>
        /// <param name="email">Email.</param>
        /// <returns></returns>
        public bool CreateBasic(string firstname, string lastname, UAC_Gender gender, string id, string email, string contact, string usertype, string familyID)
        {
            if (basic == null)
            {
                basic = new UAC_BasicDetails(firstname, lastname, gender, id, email, contact, usertype, familyID);
                return true;
            }
            else return false;
        }

        /// <summary>
        /// Advanced Details.
        /// </summary>
        /// <param name="dob">Date Of Birth.</param>
        /// <param name="homeLang">Home Language.</param>
        /// <param name="nationality">Nationality.</param>
        /// <param name="homeAdd">Home Address.</param>
        /// <param name="postalAdd">Postal Address.</param>
        /// <param name="city">City.</param>
        /// <param name="country">Country.</param>
        /// <param name="code">Code.</param>
        /// <param name="medAid">Medical Aid.</param>
        /// <param name="medAidnum">Medical Aid Number.</param>
        /// <param name="ethnicGroup">Ethnic Group.</param>
        /// <param name="disabilities">Disabilities.</param>
        /// <returns></returns>
        public bool CreateAdvanced(DateTime dob, string homeLang, string nationality,
            string homeAdd, string postalAdd, string city, string country, string code,
            string medAid, string medAidnum, UAC_EthnicGroup ethnicGroup,
            UAC_Disabilities disabilities)
        {
            if (advanced == null)
            {
                advanced = new UAC_AdvancedDetails(dob, homeLang, nationality, homeAdd, postalAdd, city, country, code, medAid, medAidnum, ethnicGroup, disabilities);
                return true;
            }
            else return false;

        }

        /// <summary>
        /// marketing Details.
        /// </summary>
        /// <param name="marketingMedium">The marketing medium.</param>
        /// <param name="marketingComment">The marketing comment.</param>
        /// <param name="marktingDateOfSubmission">The markting date of submission.</param>
        /// <param name="occupationStatus">The occupation status.</param>
        /// <param name="highschoolDetails">The highschool details.</param>
        /// <returns></returns>
        public bool CreateMarket(string marketingMedium, string marketingComment,
            DateTime marktingDateOfSubmission, UAC_OccupationStatus occupationStatus,
            string highschoolDetails)
        {
            if (marketingDetails == null)
            {
                marketingDetails = new UAC_MarketingDetails(marketingMedium, marketingComment, marktingDateOfSubmission, occupationStatus, highschoolDetails);
                return true;
            }
            else return false;
        }

        /// <summary>
        /// Educational Details.
        /// </summary>
        /// <param name="edulist">The edulist.</param>
        /// <returns></returns>
        public bool CreateEdu(List<I_UAC_InstitutionBackground> edulist)
        {
            if (edu == null)
            {
                edu = new UAC_EducationalBackground(edulist);
                return true;
            }
            else return false;

        }

        /// <summary>
        /// Registers the specified User.
        /// </summary>
        /// <param name="AccountPass">The account pass.</param>
        /// <param name="SecretQuestion">The secret question.</param>
        /// <param name="SecretPassword">The secret password.</param>
        /// <returns></returns>
        public bool Register(string AccountPass, string SecretQuestion, string SecretPassword, UAC_BasicDetails basic, UAC_AdvancedDetails advanced)
        {
            UAC_RegistrationDetails reg = new UAC_RegistrationDetails();
            UAC_Account newuser = reg.RegisterNewAccount(basic, advanced, null, null, AccountPass, SecretQuestion, SecretPassword);
            newuser.AccountUID = UAC_UserManagement.GenerateAccountID();
            userlist.Add(newuser);
            //Save(newuser);
            return true;
        }

        /// <summary>
        /// Registers the specified User.
        /// </summary>
        /// <param name="AccountPass">The account pass.</param>
        /// <param name="SecretQuestion">The secret question.</param>
        /// <param name="SecretPassword">The secret password.</param>
        /// <returns></returns>
        public bool Register(string AccountPass, string SecretQuestion, string SecretPassword, UAC_BasicDetails basic, UAC_AdvancedDetails advanced, UAC_MarketingDetails marketingDetails, UAC_EducationalBackground edu)
        {
            UAC_RegistrationDetails reg = new UAC_RegistrationDetails();

            UAC_Account newuser = reg.RegisterNewAccount(basic, advanced, marketingDetails, edu, AccountPass, SecretQuestion, SecretPassword);
            newuser.Status = true;
            userlist.Add(newuser);
            //Save(newuser);
            return true;
        }

        #endregion

        #region Validation

        /// <summary>
        /// Validates the specified email.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="pass">The pass.</param>
        /// <returns></returns>
        public bool Validate(string email, string pass)
        {
            foreach (UAC_Account user in userlist)
            {
                if (email == user.BasicAccountDetails.Email && pass == user.Password)
                {
                    return true;
                }
            }
            return false;
        }

        #endregion

        #region Public Methods

        public static string GenerateAccountID()
        {
            string AccountID = "U" + Num.ToString("00000000");
            Num++;
            return AccountID;
        }

        public UAC_Account Search(string SearchEmail)
        {
            //GetUserSingle(SearchEmail);
            return SingleUser;
        }

        public bool Delete(string Email)
        {
            UAC_Account DeleteUser = new UAC_Account();
            for (int i = 0; i >= userlist.Count() - 1; i++)
            {
                if (userlist[i].BasicAccountDetails.Email == Email)
                {
                    DeleteUser = userlist[i];
                    userlist.RemoveAt(i);
                    //Delete(DeleteUser);
                    return true;
                }
            }
            return false;
        }

        public bool Update(UAC_Account updated)
        {
            if (updated != null)
            {
                for (int i = 0; i > userlist.Count(); i++)
                {
                    if (userlist[i].AccountUID == updated.AccountUID)
                    {
                        userlist[i] = updated;
                        //UpdateSave(updated);
                    }
                }
            }
            return true;
        }

        public void EnableAccount(UAC_Account thisuser)
        {
            thisuser.Status = true;
        }

        public void DisableAccount(UAC_Account thisuser)
        {
            thisuser.Status = false;
        }

        public bool SettingFlags()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Engine Implementation

        #endregion

        #region UAC_UserManagement_Control Implementation

        //// Get Single User
        //public void GetUserSingle(string email)
        //{
        //    SingleUser = umControl.GetUAC_UserManagement(email);
        //}

        //// Get User iList
        //public void LoadUserList()
        //{
        //    userlist = umControl.GetUAC_UserManagements();
        //}

        //// Update or Save User
        //public void UpdateSave(UAC_UserManagement user)
        //{
        //    umControl.SaveOrUpdateUAC_UserManagement(user);
        //}

        //// Delete User
        //public void Delete(UAC_UserManagement user)
        //{
        //    umControl.DeleteUAC_UserManagement(user);
        //}

        #endregion
    }
}
